package com.kzd.gtboon.custom

import android.annotation.SuppressLint
import android.app.Activity
import android.content.Context
import android.content.res.ColorStateList
import android.util.AttributeSet
import android.util.TypedValue
import android.view.Gravity
import android.view.View
import android.widget.FrameLayout
import android.widget.TextView
import androidx.core.content.ContextCompat
import com.blankj.utilcode.util.BarUtils
import com.blankj.utilcode.util.ColorUtils
import com.blankj.utilcode.util.ConvertUtils
import com.kzd.gtboon.R

@SuppressLint("CustomViewStyleable")
class ToolbarLayout @JvmOverloads constructor(
    context: Context, attrs: AttributeSet? = null, defStyle: Int = 0,
) : FrameLayout(context, attrs, defStyle) {

    private val leftTextView: TextView by lazy {
        TextView(context).apply {
            setOnClickListener {
                if (context is Activity) context.onBackPressed()
            }
            layoutParams = LayoutParams(
                LayoutParams.WRAP_CONTENT, LayoutParams.MATCH_PARENT,
                (Gravity.START or Gravity.CENTER_VERTICAL)
            )
            if (leftDrawableResId > 0) setComponentDrawable(leftDrawableResId, this)
            else text = leftTextContent

            visibility = if (showLeft)
                View.VISIBLE else View.INVISIBLE
            gravity = Gravity.CENTER
            setTextSize(TypedValue.COMPLEX_UNIT_PX, leftTextSize)
            setPadding(ConvertUtils.dp2px((15f)), (0), ConvertUtils.dp2px((15f)), (0))
            val color = leftTextColor ?: ContextCompat
                .getColorStateList(context, R.color.colorMain)
            setTextColor(color)
        }
    }
    private val titleTextView: TextView by lazy {
        TextView(context).apply {
            layoutParams = LayoutParams(
                LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, Gravity.CENTER
            )
            gravity = Gravity.CENTER
            text = titleTextContent
            setTextSize(TypedValue.COMPLEX_UNIT_PX, titleTextSize)
//            val color = titleTextColor ?: ContextCompat
//                .getColorStateList(context, R.color.colorMain)
            setTextColor(titleTextColor)
        }
    }
    private val rightTextView: TextView by lazy {
        TextView(context).apply {

            layoutParams = LayoutParams(
                LayoutParams.WRAP_CONTENT,
                LayoutParams.MATCH_PARENT,
                (Gravity.END or Gravity.CENTER_VERTICAL)
            )
            gravity = Gravity.CENTER
            setTextSize(TypedValue.COMPLEX_UNIT_PX, rightTextSize)
            if (rightDrawableResId > 0) setComponentDrawable(rightDrawableResId, this)
            else text = rightTextContent
            if (rightBackgroundResId > 0) setBackgroundResource(rightBackgroundResId)
            visibility = if (showRight)
                View.VISIBLE else View.INVISIBLE
            setPadding(ConvertUtils.dp2px((15f)), (0), ConvertUtils.dp2px((15f)), (0))
            val color = rightTextColor ?: ContextCompat
                .getColorStateList(context, R.color.colorMain)
            setTextColor(color)
        }
    }

    private var showLeft = false
    private var leftTextColor: ColorStateList? = null
    private var leftTextSize = 0F
    private var leftTextContent: String? = null
    private var leftDrawableResId = 0
    private var titleTextContent: String? = null
    private var titleTextSize = 0F
    private var titleTextColor: ColorStateList? = null
    private var showRight = false
    private var rightTextColor: ColorStateList? = null
    private var rightTextSize = 0F
    private var rightTextContent: String? = null
    private var rightDrawableResId = 0
    private var rightBackgroundResId = 0

    init {
        with(context.obtainStyledAttributes(attrs, R.styleable.TitleBar)) {
            showLeft = getBoolean(R.styleable.TitleBar_tb_show_left, false)
            leftTextColor = getColorStateList(R.styleable.TitleBar_tb_left_text_color)
                ?: ContextCompat.getColorStateList(context, R.color.black)
            leftTextSize = getDimension(
                R.styleable.TitleBar_tb_left_text_size,
                ConvertUtils.sp2px(12f).toFloat()
            )
            leftTextContent = getString(R.styleable.TitleBar_tb_left_text)
            leftDrawableResId = getResourceId(R.styleable.TitleBar_tb_left_drawable, 0)

            titleTextContent = getString(R.styleable.TitleBar_tb_title_text)
            titleTextSize = getDimension(
                R.styleable.TitleBar_tb_title_text_size, ConvertUtils.sp2px(17f).toFloat()
            )
            titleTextColor = getColorStateList(R.styleable.TitleBar_tb_title_text_color)
                ?: ContextCompat.getColorStateList(context, R.color.black)

            showRight = getBoolean(R.styleable.TitleBar_tb_show_right, false)
            rightTextColor = getColorStateList(R.styleable.TitleBar_tb_right_text_color)
                ?: ContextCompat.getColorStateList(context, R.color.black)
            rightTextSize = getDimension(
                R.styleable.TitleBar_tb_right_text_size, ConvertUtils.sp2px(12f).toFloat()
            )
            rightTextContent = getString(R.styleable.TitleBar_tb_right_text)
            rightDrawableResId = getResourceId(R.styleable.TitleBar_tb_right_drawable, 0)
            rightBackgroundResId = getResourceId(R.styleable.TitleBar_tb_right_background, 0)

            recycle()
        }
        addView(leftTextView)
        addView(titleTextView)
        addView(rightTextView)
    }

    private fun setComponentDrawable(drawableResId: Int, target: TextView) {
        ContextCompat.getDrawable(context, drawableResId)?.run {
            setBounds(0, 0, intrinsicWidth, intrinsicHeight)
            target.setCompoundDrawables(this, null, null, null)
        }
    }

    fun setTitleTextContent(title: String?) {
        titleTextView.text = title
    }

    fun setLeftVisibility(visibility: Int) {
        leftTextView.visibility = visibility
    }

    fun setOnClickRightTextView(onClick: OnClickListener) {
        rightTextView.setOnClickListener(onClick)
    }

}